package com.xhs.train;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;

public class CuratorDemo {
    public static void main(String[] args) throws Exception{

        CuratorFramework zkClient = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2180,127.0.0.1:2181,127.0.0.1:2182")
                .sessionTimeoutMs(30000)
                .connectionTimeoutMs(30000)
                .canBeReadOnly(true)
                .namespace("demo_service")
                .retryPolicy(new ExponentialBackoffRetry(1000,Integer.MAX_VALUE))
                .defaultData(null)
                .build();

        zkClient.start();

        if(zkClient.checkExists().forPath("/node_a/node_b") == null){
            String s = zkClient.create()
                    .creatingParentContainersIfNeeded()
                    .withMode(CreateMode.PERSISTENT)
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                    .forPath("/node_a/node_b", "aa".getBytes());
            System.out.println(s);
        }

        Stat stat = zkClient.checkExists().forPath("/node_a/node_b");
        System.out.println(stat);


        zkClient.delete().deletingChildrenIfNeeded().forPath("/node_a");

        //Thread.sleep(30000000);
        zkClient.close();
    }
}
